package com.zuimei.zuimei.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewPropertyAnimator;
import android.widget.RelativeLayout;

import com.zuimei.zuimei.utils.ScreenUtil;

/**
 * Created by Jogger on 2017/1/18.
 * 可滚动的相对布局
 */

public class ScrollableRelativeLayout extends RelativeLayout {
    private boolean isMoveAble = true;
    private boolean isLeft = false;//判断左边是否有显示部分
    private int mStartX;
    private int mDx;
    private int mMaxWidth = 0;//最大宽度

    public ScrollableRelativeLayout(Context context) {
        this(context, null);
    }

    public ScrollableRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollableRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mMaxWidth = ScreenUtil.getSrceenWidth(context);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (isMoveAble) {//可以移动
                    mDx = (int) event.getX();
                    int moveX = mDx - mStartX;
                    if (moveX > 0) {
                        //右移
                        isLeft = true;//左边有显示部分
                        layout(getLeft() + moveX, getTop(), getRight() + moveX, getBottom());
                    } else {
                        //左移
                        if (isLeft) {//已经有显示部分
                            layout(getLeft() + moveX, getTop(), getRight() + moveX, getBottom());
                            if (getLeft() == 0 || getRight() < mMaxWidth) {//移动到最初的位置
                                layout(0, getTop(), mMaxWidth, getBottom());
                                isLeft = false;
                            }
                        }
                        layout(getLeft(), getTop(), getRight(), getBottom());//没有显示部分，位置不变
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                float upX = getX();
                ViewPropertyAnimator animate = this.animate();
                if (upX > mMaxWidth * 0.4) {
                    //超过屏幕一半
                    Log.d("TAG", "可以结束activity");
                    animate.translationXBy(upX).x(0).setDuration((int) (upX /
                            mMaxWidth * 2000)).start();
                } else {
                    Log.d("TAG", "---" + (int) (upX /
                            mMaxWidth * 1000) + "----" + (mMaxWidth * 0.4) + "---upx:" + upX);
                    //归位
                    animate.translationXBy(upX).x(0).setDuration((int) (upX /
                            mMaxWidth * 2000)).start();
                }
                break;
        }
        return true;
    }
}
